package io.choerodon.iam.infra.config;

import static java.util.Locale.SIMPLIFIED_CHINESE;

import org.hzero.core.message.MessageAccessor;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * @author shinan.chen
 * @date 2018/9/27
 */
@Configuration
public class BeanConfiguration {

    @Bean
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        return modelMapper;
    }

    @Bean
    public LocalValidatorFactoryBean getValidator() {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        ReloadableResourceBundleMessageSource messageSource
                = new ReloadableResourceBundleMessageSource();
        messageSource.addBasenames(MessageAccessor.getBasenames());
        messageSource.addBasenames("classpath:messages/messages_ciam");
        messageSource.addBasenames("classpath:messages/messages_bus");
        messageSource.addBasenames("classpath:messages/messages_hand");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setDefaultLocale(SIMPLIFIED_CHINESE);
        bean.setValidationMessageSource(messageSource);
        return bean;
    }
}